<?php
class FileAction extends Action
{
	public function excel()
{

require_once '../Classes/PHPExcel.php';


$objPHPExcel = new PHPExcel();	
//$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);//excel—2007格式
$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
$objActSheet = $objPHPExcel->getActiveSheet();
 



$result=json_decode($_REQUEST['data']);

$header=json_decode($_REQUEST['header']);

//dump($header);
//dump($result);

$A='A';
$j=0;

foreach($header as $key=>$val)
{

$objActSheet->setCellValue($A.'1', $val->header);
$F[$j]=$val->field;
$j++;
$A++;
}

$k=2;
foreach($result as $key=>$val){

$A='A';
for($i=0;$i<count($F);$i++)
	{
		$objActSheet->setCellValue($A.$k, $val->$F[$i]);
		$A++;
	}

$k++;

}

header("Pragma: public");
header("Expires: 0");
header('Cache-Control:must-revalidate, post-check=0, pre-check=0');
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename="'.date('Y-m-d_H-i-s').'.xls"');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
}


public function exportexcel()
{
	
header("Content-type:charset=utf-8");
$PID=$_REQUEST['pid'];
$DB_Server = "localhost"; 
$DB_Username = "root"; 
$DB_Password = ""; 
$DB_DBName = "test"; 
$DB_TBLName = $_REQUEST['tablename']; 

$savename = $_REQUEST['filename']; 
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password) or die("Couldn't connect."); 
mysql_query("Set Names 'gbk'"); 
$file_type = "vnd.ms-excel"; 
$file_ending = "xls"; 
header("Content-Type: application/$file_type;charset=big5"); 
header("Content-Disposition: attachment; filename=".$savename.".$file_ending"); 
//header("Pragma: no-cache"); 

$now_date = date("Y-m-j H:i:s"); 
$title = "数据库名:$DB_DBName,数据表:$DB_TBLName,备份日期:$now_date"; 

if($_REQUEST['pid'])
	$sql = "Select * from $DB_TBLName where project_pid ='$PID' "; 
else {
	$sql = "Select * from $DB_TBLName ";
}
$ALT_Db = @mysql_select_db($DB_DBName, $Connect) or die("Couldn't select database"); 
$result = @mysql_query($sql,$Connect) or die(mysql_error()); 

//echo iconv('utf-8','gbk',$title)."\n"; 
$sep = "\t"; 
for ($i = 0; $i < mysql_num_fields($result); $i++) { 
echo mysql_field_name($result,$i) . "\t"; 
} 
print("\n"); 
$i = 0; 
while($row = mysql_fetch_row($result)) { 
$schema_insert = ""; 
for($j=0; $j<mysql_num_fields($result);$j++) { 
if(!isset($row[$j])) 
$schema_insert .= "NULL".$sep; 
elseif ($row[$j] != "") 
$schema_insert .= "$row[$j]".$sep; 
else 
$schema_insert .= "".$sep; 
} 
$schema_insert = str_replace($sep."$", "", $schema_insert); 
$schema_insert .= "\t"; 
print(trim($schema_insert)); 
print "\n"; 
$i++; 
} 

	//$savename=iconv("UTF-8", "GB2312", "C:/wamp/www/a.txt");
	
	//$showname="b.txt";
	
	//import('@.ORG.Http'); 
    //Http::download($savename,$showname); 
}	 
public function down()
{
	$orginalname=$_GET["name"];
	$level=$_GET["level"];
	if ($orginalname=='')
	{
		echo "<script>alert('无文件名!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";
	}
	$file=D("file");
	$condition['orginalname'] = $orginalname;
	$condition['level']= $level;
	$list=$file->where($condition)->select();
	$savename=iconv("UTF-8", "GB2312", $list[0]['path']);
	//$savename=iconv("UTF-8", "GB2312", "C:/wamp/www/ivycapital/Public/example/uploads/殷三三/啊啊.txt");
	$showname=iconv("UTF-8", "GB2312", $list[0]['orginalname']);
	//$showname=$list[0]['orginalname'];
	import('@.ORG.Http'); 
    Http::download($savename,$showname); 
}


public function downall()
{
	$project=$_GET['project'];
	$level=$_GET['level'];
	//$level=$_GET['level'];
	if ($project==''){echo "<script>alert('无项目名!');location.href='".$_SERVER["HTTP_REFERER"]."';</script>";}
	//$file=D("file");
	//$condition['project'] = $project;
	//$list=$file->where($condition)->select();
	
	$con = mysql_connect("localhost","root","");
	if (!$con)
 	 {
 	 die('Could not connect: ' . mysql_error());
	  }
	mysql_select_db("test", $con);
	if($level)
	$sql="select * from file where project='".$project."' and level='".$level."'";
	else 
	$sql="select * from file where project='".$project."'";
			
	$result=mysql_query($sql,$con);
	
	
	
	$zip=new ZipArchive();
	
	//$zipath1='C:/wamp/www/ivycapital/Public/example/uploads/'.$project.'/'.$level.'/'.$project.'-'.$level.'.zip';
	if($level)
	$zipath1='C:/wamp/www/ivyproject/Public/uploads/'.$project.'/'.$level.'/'.$project.'-'.$level.'.zip';
	else
	$zipath1='C:/wamp/www/ivyproject/Public/uploads/'.$project.'/'.$project.'.zip';
		
	$zipath=iconv("UTF-8", "GB2312",$zipath1);
	
	if($level)
	$zipname1=$project.'-'.$level.'.zip';
	else 
	$zipname1=$project.'.zip';
	
	$zipname=iconv("UTF-8", "GB2312", $zipname1);
	$res=$zip->open($zipath,ZipArchive::OVERWRITE);
	
	
	if ($res==TRUE)
	{	
    while ($row= mysql_fetch_array($result))
	{
		//这里将文件打包成zip格式
		$realpath=iconv("UTF-8","GB2312",$row['path']);
		$name=iconv("UTF-8", "GB2312", $row['orginalname']);
		//$name=$row['orginalname'];
		$zip->addFile($realpath,$name);		
	}
	$zip->close();
	import('@.ORG.Http');
	Http::download($zipath,$zipname);
	}
	else
	{
		echo "zip文件创建失败";
	}
}
}
?>